package com.kotlin.lib.coroutines

import kotlinx.coroutines.*

fun main() = runBlocking<Unit> {
    val job = launch(Dispatchers.Default) {
        repeat(1000) {
            println("i'm sleep ...$it")
            delay(200)
        }

    }
    delay(1500)
    println("i'm cancel job")
    job.cancelAndJoin()
    println("i'm quite")

    println("**********************************")
    var nextPrintTime = System.currentTimeMillis()
    val job2 = launch(Dispatchers.Default) {
        var i = 0
        while (/*i < 5 &&*/ isActive) {
            if (nextPrintTime <= System.currentTimeMillis()) {
                println("i'm execute...$i")
                i++
                nextPrintTime += 500
            }
        }
    }
    delay(1300)
    println("i'm tired of waiting.")
    job2.cancelAndJoin()
    println("i can quite now")
}